use strict;
use warnings;

package Contact::Phone;
use Country;

use base qw(My::DB::Object);

__PACKAGE__->meta->setup(
    table   => 'Phone',
    columns => [
        'id'         => { 'type' => 'serial',    'primary_key' => 1,  'not_null' => 1 },
        'country_id' => { 'type' => 'varchar',   'length'      => 2,  'not_null' => 1 },
        'number'     => { 'type' => 'varchar',   'length'      => 64, 'not_null' => 1 },
        'type'       => { 'type' => 'varchar',   'length'      => 32, 'not_null' => 1 },
        'active'     => { 'type' => 'boolean',   'not_null'    => 1,  'default'  => 1 },
        'created'    => { 'type' => 'timestamp', 'not_null'    => 1,  'default'  => 'now' },
        'retired'    => { 'type' => 'timestamp', 'not_null'    => 0 },
    ],
    relationships => [
        country => { type => 'one to one', class => 'Country', column_map => { 'country_id' => 'id' }, },
        owners  => {
            type        => 'many to many',
            map_class   => 'Contact::PersonPhoneMap',
            map_from    => 'phone',
            map_to      => 'person',
            add_methods => ['count'],
            }
        ,
    ],
);

__PACKAGE__->meta->column('number')->add_trigger( event => 'on_set', code => \&format_number_by_country );

sub format_number_by_country {
    my $self       = shift;
    my $country_id = $self->country_id;

    if ($country_id) {
        my $country = Country->new( 'id' => $country_id );
        my $rv = $country->load( 'speculative' => 1 );
        if ($rv) {
            my $calling_prefix       = $country->calling_prefix;
            my $calling_prefix_regex = $calling_prefix;

            # the leading '+' character in the calling prefix needs to be handled
            $calling_prefix_regex =~ s/^\+/\\\+/;

            if ( $self->number && $calling_prefix && $self->number !~ /^$calling_prefix_regex/ ) {

                # not properly formatted
                my $original_number = $self->number;
                $original_number =~ s/[^\d\+]//g;

                my $calling_prefix_digits = $calling_prefix;
                $calling_prefix_digits =~ s/^\+//;
                if ( $original_number =~ /^$calling_prefix_digits/ ) {

                    # the country code is there, all that is missing is just the '+'
                    $self->number( '+' . $original_number );
                }
                elsif ( $country->intl_dial_truncate_lead_zero && $original_number =~ /^[0]/ ) {

                    # the number starts with a digit, which we need to strip first
                    my $stripped_number = $original_number;
                    $stripped_number =~ s/^[0]//;
                    $self->number( $calling_prefix . $stripped_number );
                } ## end elsif ( $country->intl_dial_truncate_lead_zero...
                else {
                    $self->number( $calling_prefix . $original_number );
                }
            } ## end if ( $self->number && $calling_prefix && $self...
        } ## end if ($rv)
    } ## end if ($country_id)
} ## end sub format_number_by_country

1;
